#include <iostream>
#include "FromGoogleMapXMLToDistanceTable.h"

// Constructeur
FromGoogleMapXMLToDistanceTable::FromGoogleMapXMLToDistanceTable() : LwSaxParser() {
    state = UNKNOWN;
    adresseCourante = "";
    adresses = NULL;
    ligne = NULL;
    distanceMatrix = NULL;
}


FromGoogleMapXMLToDistanceTable::~FromGoogleMapXMLToDistanceTable() {
    void parseDocument(char* param);
    void on_end_document();
    void on_end_element(const Glib::ustring& param);
    void on_characters(const Glib::ustring& text);
    void on_start_document();
    void on_start_element(const Glib::ustring& name, const AttributeList& attributes);
}


std::vector<std::string> * FromGoogleMapXMLToDistanceTable::getAdresses() {
   
    return adresses;
}

std::vector< std::vector<int> > * FromGoogleMapXMLToDistanceTable::getDistances() {
   
    return distanceMatrix;
    
}

// A compléter en implémentant les méthodes à redéfinir de LwSaxParser

void FromGoogleMapXMLToDistanceTable::parseDocument(char * filename) {
 try
    {
        std::cout << "Lecture du fichier " << filename << " ligne par ligne..." << std::endl;
        std::string line;
        std::ifstream inputStream(filename);
        while (std::getline(inputStream, line)) {
            parse_chunk(line);
        }
        finish_chunk_parsing();
        
        std::cout << "Fin de la lecture du fichier " << filename << "." << std::endl;
      }
      catch(const xmlpp::exception& ex)
       {
           std::cout << "libxml++ exception: " << ex.what() << std::endl;
       }
}

void FromGoogleMapXMLToDistanceTable::on_start_element(const Glib::ustring& name, const AttributeList& attributes)
{
  if(name == "origin_address")
  {
  	state = ORIGIN_ADDRESS;
  }
  else if(name == "row"){
  	state = ROW;
  	ligne = new std::vector<int>();
  }
  else if(name == "element"){
  	state = ELEMENT;
  }
  else if(name == "distance"){
  	state = DISTANCE;
  }
  else if(name == "value" && state == DISTANCE){
  	state = VALUE;
  }
  else{
  	state = UNKNOWN;
  }

}

void FromGoogleMapXMLToDistanceTable::on_end_element(const Glib::ustring& name)
{
  if(name == "origin_address")
  {
  	adresses->push_back(adresseCourante);
  	state = START;
  }
  else if(name == "row"){
  	state = ORIGIN_ADDRESS;
  	std::vector<int> ligne1;
    	for (int i = 0; i < ligne->size(); i++) {
		ligne1.push_back(ligne->at(i));
    	}
  	distanceMatrix->push_back(ligne1);
  	
  	delete ligne;
  }
  else if(name == "element"){
  	state = ROW;
  	
  }
  else if(name == "distance"){
  	state = ELEMENT;
  }
  else if(name == "value" && state == VALUE){
  	state = DISTANCE;
  }
  else{
  	state = UNKNOWN;
  }
}

void FromGoogleMapXMLToDistanceTable::on_characters(const Glib::ustring& text)
{
  if(state == VALUE){
  	int val = atoi(text.c_str());
  	ligne->push_back(val);
  }
  else if(state == ORIGIN_ADDRESS){
  	adresseCourante = text;
  	
  	
  }
}

void FromGoogleMapXMLToDistanceTable::on_start_document() {
	std::cout << "Appel de la méthode: on_start_document()" << std::endl;
	state = START;
	distanceMatrix =  new std::vector< std::vector<int> >(); 
	adresses = new std::vector<std::string>();
}

void FromGoogleMapXMLToDistanceTable::on_end_document() {
	std::cout << "Appel de la méthode: on_end_document()" << std::endl;
}
